<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MadiChat - 聊天</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            height: 100vh;
            overflow: hidden;
            display: flex;
            flex-direction: column;
            background-color: #f8f9fa;
        }
        /* 思考中的动画样式 */
        .thinking .dot-1, .thinking .dot-2, .thinking .dot-3 {
            animation: pulse 1.5s infinite;
            opacity: 0;
        }
        .thinking .dot-1 { animation-delay: 0s; }
        .thinking .dot-2 { animation-delay: 0.3s; }
        .thinking .dot-3 { animation-delay: 0.6s; }
        @keyframes pulse {
            0% { opacity: 0; }
            50% { opacity: 1; }
            100% { opacity: 0; }
        }
        .thinking-container {
            background-color: #f8f9fa;
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 10px;
            font-size: 0.9em;
            color: #6c757d;
        }
        .thinking-process {
            color: #6c757d;
            font-style: italic;
            margin-bottom: 15px;
            padding: 8px;
            background-color: #f0f0f0;
            border-radius: 8px;
            font-size: 0.9em;
        }
        .response-content {
            margin-top: 15px;
            border-top: 1px dashed #e9ecef;
            padding-top: 10px;
        }
        .chat-container {
            display: flex;
            flex: 1;
            overflow: hidden;
        }
        .sidebar {
            width: 280px;
            background-color: #fff;
            border-right: 1px solid #e9ecef;
            display: flex;
            flex-direction: column;
            transition: all 0.3s;
        }
        .sidebar-header {
            padding: 1rem;
            border-bottom: 1px solid #e9ecef;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .sidebar-header h5 {
            margin: 0;
        }
        .new-chat-btn {
            width: auto;
            margin: 1rem;
            border-radius: 0.5rem;
        }
        .chat-list {
            flex: 1;
            overflow-y: auto;
            padding: 0.5rem;
        }
        .chat-item {
            padding: 0.75rem;
            border-radius: 0.5rem;
            margin-bottom: 0.5rem;
            cursor: pointer;
            transition: background-color 0.2s;
            position: relative;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .chat-item:hover {
            background-color: #f0f0f0;
        }
        .chat-item.active {
            background-color: #e9ecef;
        }
        .chat-item .title {
            font-weight: 500;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            flex: 1;
        }
        .chat-item .date {
            font-size: 0.75rem;
            color: #6c757d;
        }
        .chat-item-content {
            flex: 1;
            overflow: hidden;
        }
        .chat-item-menu {
            visibility: hidden; /* 默认隐藏 */
            margin-left: 8px;
            color: #6c757d;
            width: 32px; /* 增加点击区域 */
            height: 32px; /* 增加点击区域 */
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            transition: all 0.2s ease; /* 添加过渡效果 */
            background-color: transparent;
        }
        
        .chat-item:hover .chat-item-menu {
            visibility: visible;
            background-color: #e9ecef; /* 默认就有背景色 */
        }
        
        .chat-item-menu:hover {
            background-color: #d1d3d6; /* 更深的背景色 */
            color: #0d6efd; /* 更鲜艳的颜色 */
            transform: scale(1.1); /* 轻微放大效果 */
        }
        
        /* 活跃状态的菜单按钮 */
        .chat-item-menu[aria-expanded="true"] {
            visibility: visible;
            background-color: #d1d3d6;
            color: #0d6efd;
        }
        
        /* 移动设备样式 */
        @media (max-width: 768px) {
            .chat-item-menu {
                visibility: visible; /* 在移动设备上始终显示 */
                opacity: 0.7; /* 但稍微透明 */
            }
            
            /* 添加长按效果的视觉反馈 */
            .chat-item-menu:active {
                background-color: #d1d3d6;
                color: #0d6efd;
                transform: scale(1.1);
            }
        }
        
        .dropdown-menu {
            min-width: 150px; /* 稍微加宽 */
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
            border: none;
            border-radius: 0.5rem;
            /* 修改动画，避免位置变化 */
            animation: scaleIn 0.2s ease; /* 使用缩放动画替代fadeIn */
            /* 禁用Bootstrap的自动定位 */
            position: fixed !important; /* 使用fixed定位 */
            transform-origin: top right; /* 设置变换原点 */
            margin: 0 !important; /* 移除外边距 */
        }
        
        /* 替换fadeIn为scaleIn动画 */
        /* 添加上方显示菜单的样式 */
        .dropdown-menu-up {
            transform-origin: bottom right !important;
        }
        
        /* 修改动画，使其适应上下两种方向 */
        @keyframes scaleIn {
            from { opacity: 0; transform: scale(0.95); }
            to { opacity: 1; transform: scale(1); }
        }
        .dropdown-item {
            padding: 0.5rem 1rem;
            font-size: 0.875rem;
            transition: all 0.2s ease; /* 添加过渡效果 */
        }
        
        .dropdown-item:hover {
            background-color: #f0f4ff; /* 更明显的悬停背景 */
            color: #0d6efd;
            transform: translateX(3px); /* 轻微的移动效果 */
        }
        .chat-item:hover .chat-item-menu {
            visibility: visible;
        }
        .chat-item-menu:hover {
            background-color: #e9ecef;
            color: #4e73df;
        }
        .dropdown-menu {
            min-width: 120px;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
            border: none;
            border-radius: 0.5rem;
        }
        .dropdown-item {
            padding: 0.5rem 1rem;
            font-size: 0.875rem;
        }
        .dropdown-item:hover {
            background-color: #f8f9fa;
            color: #4e73df;
        }
        .sidebar-footer {
            padding: 1rem;
            border-top: 1px solid #e9ecef;
        }
        .main-content {
            flex: 1;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }
        .chat-header {
            padding: 1rem;
            border-bottom: 1px solid #e9ecef;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .chat-title {
            font-weight: 500;
            font-size: 1.1rem;
            cursor: pointer;
            display: flex;
            align-items: center;
        }
        .chat-title:hover {
            color: #4e73df;
        }
        .chat-title .fa-edit {
            font-size: 0.9rem;
            opacity: 0.6;
        }
        .chat-title:hover .fa-edit {
            opacity: 1;
        }
        
        /* 移动端适配 */
        @media (max-width: 768px) {
            .sidebar {
                position: absolute;
                left: -280px;
                height: 100%;
                z-index: 1000;
            }
            .sidebar.show {
                left: 0;
            }
            .toggle-sidebar {
                display: block !important;
            }
            /* 移动端下聊天标题靠左对齐 */
            .chat-header {
                justify-content: flex-start;
            }
            .chat-title {
                margin-left: 10px;
            }
        }
        .chat-title {
            font-weight: 500;
            font-size: 1.1rem;
            cursor: pointer;
            display: flex;
            align-items: center;
        }
        .chat-title:hover {
            color: #4e73df;
        }
        .chat-title .fa-edit {
            font-size: 0.9rem;
            opacity: 0.6;
        }
        .chat-title:hover .fa-edit {
            opacity: 1;
        }
        .model-selector {
            display: flex;
            align-items: center;
        }
        .model-selector select {
            margin-left: 0.5rem;
            border-radius: 0.5rem;
        }
        .chat-messages {
            flex: 1;
            overflow-y: auto;
            padding: 1rem;
            background-color: #f8f9fa;
        }
        .message {
            max-width: 80%;
            margin-bottom: 1rem;
            padding: 1rem;
            border-radius: 1rem;
            position: relative;
        }
        .message.user {
            background-color: #4e73df;
            color: white;
            margin-left: auto;
            border-bottom-right-radius: 0.25rem;
        }
        .message.ai {
            background-color: #fff;
            border: 1px solid #e9ecef;
            margin-right: auto;
            border-bottom-left-radius: 0.25rem;
        }
        .message .avatar {
            width: 2rem;
            height: 2rem;
            border-radius: 50%;
            position: absolute;
            bottom: -0.5rem;
        }
        .message.user .avatar {
            right: -0.5rem;
        }
        .message.ai .avatar {
            left: -0.5rem;
        }
        .message-time {
            font-size: 0.75rem;
            color: #6c757d;
            margin-top: 0.5rem;
        }
        .message.user .message-time {
            color: rgba(255, 255, 255, 0.8);
        }
        .chat-input {
            padding: 1rem;
            border-top: 1px solid #e9ecef;
            background-color: #fff;
        }
        .input-group {
            border-radius: 1.5rem;
            overflow: hidden;
            background-color: #f8f9fa;
            padding: 0.5rem;
        }
        .form-control {
            border: none;
            background-color: transparent;
            resize: none;
        }
        .form-control:focus {
            box-shadow: none;
        }
        .input-group-text {
            background-color: transparent;
            border: none;
            cursor: pointer;
            color: #6c757d;
        }
        .input-group-text:hover {
            color: #4e73df;
        }
        .send-btn {
            border-radius: 50%;
            width: 2.5rem;
            height: 2.5rem;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #4e73df;
            color: white;
            border: none;
        }
        .send-btn:hover {
            background-color: #2e59d9;
        }
        .options-bar {
            display: none;
            padding: 0.5rem 1rem;
            background-color: #fff;
            border-top: 1px solid #e9ecef;
        }
        .option-item {
            display: none;
            align-items: center;
            margin-right: 1rem;
            cursor: pointer;
            color: #6c757d;
        }
        .option-item:hover {
            color: #4e73df;
        }
        .option-item input {
            margin-right: 0.25rem;
        }
        
        /* 移动端适配 */
        @media (max-width: 768px) {
            .sidebar {
                position: absolute;
                left: -280px;
                height: 100%;
                z-index: 1000;
            }
            .sidebar.show {
                left: 0;
            }
            .toggle-sidebar {
                display: block !important;
            }
        }
        
        /* iPhone 15 Pro 模拟样式 */
        @media (max-width: 430px) {
            body {
                max-width: 430px;
                margin: 0 auto;
                border-radius: 2rem;
                overflow: hidden;
                box-shadow: 0 0 2rem rgba(0, 0, 0, 0.1);
            }
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <!-- 侧边栏 -->
        <div class="sidebar" id="sidebar">
            <div class="sidebar-header">
                <h5>MadiChat</h5>
                <button class="btn btn-sm btn-outline-secondary d-md-none" onclick="toggleSidebar()">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <button class="btn btn-primary new-chat-btn">
                <i class="fas fa-plus me-2"></i>新建对话
            </button>
            <div class="chat-list">
                <div class="chat-item active">
                    <div class="chat-item-content">
                        <div class="title">关于人工智能的讨论</div>
                        <div class="date">今天 14:30</div>
                    </div>
                    <div class="chat-item-menu" 
                         data-bs-toggle="dropdown" 
                         aria-expanded="false"
                         data-bs-auto-close="true"
                         data-bs-offset="0,5"
                         data-bs-reference="self">
                        <i class="fas fa-ellipsis-v"></i>
                    </div>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="#"><i class="fas fa-edit me-2"></i>编辑名称</a></li>
                        <!-- 暂时不提供置顶功能 -->
                        <!-- <li><a class="dropdown-item" href="#"><i class="fas fa-thumbtack me-2"></i>置顶</a></li> -->
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="#"><i class="fas fa-trash-alt me-2"></i>删除</a></li>
                    </ul>
                </div>
                <div class="chat-item">
                    <div class="chat-item-content">
                        <div class="title">Python编程问题解答</div>
                        <div class="date">昨天 09:15</div>
                    </div>
                    <div class="chat-item-menu" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fas fa-ellipsis-v"></i>
                    </div>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="#"><i class="fas fa-edit me-2"></i>编辑名称</a></li>
                        <li><a class="dropdown-item" href="#"><i class="fas fa-thumbtack me-2"></i>置顶</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="#"><i class="fas fa-trash-alt me-2"></i>删除</a></li>
                    </ul>
                </div>
                <div class="chat-item">
                    <div class="chat-item-content">
                        <div class="title">旅行计划建议</div>
                        <div class="date">2023-05-20</div>
                    </div>
                    <div class="chat-item-menu" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fas fa-ellipsis-v"></i>
                    </div>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="#"><i class="fas fa-edit me-2"></i>编辑名称</a></li>
                        <li><a class="dropdown-item" href="#"><i class="fas fa-thumbtack me-2"></i>置顶</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="#"><i class="fas fa-trash-alt me-2"></i>删除</a></li>
                    </ul>
                </div>
                <div class="chat-item">
                    <div class="chat-item-content">
                        <div class="title">健康饮食咨询</div>
                        <div class="date">2023-05-18</div>
                    </div>
                    <div class="chat-item-menu" data-bs-toggle="dropdown" aria-expanded="false">
                        <i class="fas fa-ellipsis-v"></i>
                    </div>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="#"><i class="fas fa-edit me-2"></i>编辑名称</a></li>
                        <li><a class="dropdown-item" href="#"><i class="fas fa-thumbtack me-2"></i>置顶</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="#"><i class="fas fa-trash-alt me-2"></i>删除</a></li>
                    </ul>
                </div>
            </div>
            <div class="sidebar-footer">
                <div class="d-flex">
                    <a href="profile.html" class="text-decoration-none text-dark me-4">
                        <i class="fas fa-user-circle me-2"></i>马迪
                    </a>
                    <a href="javascript:void(0)" class="text-decoration-none text-dark" id="logoutBtn">
                        <i class="fas fa-sign-out-alt me-2"></i>退出
                    </a>
                </div>
            </div>
        </div>

        <!-- 主内容区 -->
        <div class="main-content">
            <div class="chat-header">
                <button class="btn btn-sm btn-outline-secondary d-md-none toggle-sidebar" onclick="toggleSidebar()">
                    <i class="fas fa-bars"></i>
                </button>
                <div class="chat-title" id="currentChatTitle">
                    <span>关于人工智能的讨论</span>
                    <i class="fas fa-edit ms-2"></i>
                </div>
            </div>

            <div class="chat-messages">
                <div class="message ai">
                    <div class="message-content">
                        你好！我是AI助手，有什么我可以帮助你的吗？
                    </div>
                    <div class="message-time">14:30</div>
                    <img src="https://images.unsplash.com/photo-1531746020798-e6953c6e8e04?ixlib=rb-1.2.1&auto=format&fit=crop&w=100&q=80" class="avatar" alt="AI">
                </div>

                <div class="message user">
                    <div class="message-content">
                        你能告诉我关于人工智能的最新发展吗？
                    </div>
                    <div class="message-time">14:31</div>
                    <img src="https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixlib=rb-1.2.1&auto=format&fit=crop&w=100&q=80" class="avatar" alt="User">
                </div>

                <div class="message ai">
                    <div class="message-content">
                        当然可以！最近人工智能领域有几个重要的发展：<br><br>
                        1. 多模态大型语言模型的进步，如GPT-4V、Claude 3和Gemini，它们能够同时处理文本和图像输入。<br><br>
                        2. 生成式AI工具的普及，特别是在图像、音频和视频生成方面。<br><br>
                        3. AI辅助编程工具的发展，如GitHub Copilot和Amazon CodeWhisperer，提高了开发效率。<br><br>
                        4. 边缘AI技术的进步，使AI能够在低功耗设备上本地运行。<br><br>
                        你对哪个方面特别感兴趣？我可以提供更详细的信息。
                    </div>
                    <div class="message-time">14:32</div>
                    <img src="https://images.unsplash.com/photo-1531746020798-e6953c6e8e04?ixlib=rb-1.2.1&auto=format&fit=crop&w=100&q=80" class="avatar" alt="AI">
                </div>
            </div>

            <div class="options-bar">
                <div class="model-selector me-3">
                    <select class="form-select form-select-sm">
                        <option>GPT-4</option>
                        <option>Claude</option>
                        <option>Gemini</option>
                        <option>文心一言</option>
                        <option>通义千问</option>
                    </select>
                </div>
                <div class="option-item">
                    <input type="checkbox" id="deepThinking">
                    <label for="deepThinking">深度思考</label>
                </div>
                <div class="option-item">
                    <input type="checkbox" id="webSearch">
                    <label for="webSearch">联网搜索</label>
                </div>
            </div>

            <div class="chat-input">
                <div class="input-group">

                    <textarea class="form-control" placeholder="输入消息..." rows="1"></textarea>
                    <button class="input-group-text">
                        <i class="fas fa-image"></i>
                    </button>
                    <button class="send-btn ms-2">
                        <i class="fas fa-paper-plane"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- 编辑会话标题模态框 -->
    <div class="modal fade" id="editTitleModal" tabindex="-1" aria-labelledby="editTitleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="editTitleModalLabel">编辑会话名称</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="chatTitleInput" class="form-label">会话名称</label>
                        <input type="text" class="form-control" id="chatTitleInput" placeholder="请输入会话名称">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="saveTitleBtn">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 删除确认模态框 -->
    <div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-labelledby="deleteConfirmModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="deleteConfirmModalLabel">确认删除</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    该对话内容将被删除，无法恢复，是否确认删除？
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-danger" data-bs-dismiss="modal" id="confirmDeleteBtn">删除</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script src="./js/third/axios.min.js"></script>
    <script type="module" src="js/chat.js"></script>
    <!-- Toast容器 -->
    <div id="toastContainer" class="toast-container position-fixed bottom-0 end-0 p-3"></div>
    <!-- 在现有脚本之后添加 -->
    <script>
        // 修复下拉菜单位置移动的bug
        document.addEventListener('DOMContentLoaded', function() {
            // 获取所有菜单按钮
            const menuButtons = document.querySelectorAll('.chat-item-menu[data-bs-toggle="dropdown"]');
            
            menuButtons.forEach(button => {
                // 监听菜单显示事件
                button.addEventListener('shown.bs.dropdown', function() {
                    // 获取关联的下拉菜单
                    const dropdown = this.nextElementSibling;
                    if (dropdown && dropdown.classList.contains('dropdown-menu')) {
                        // 计算并设置菜单位置
                        const buttonRect = this.getBoundingClientRect();
                        const parentRect = this.closest('.chat-item').getBoundingClientRect();
                        
                        // 设置菜单位置为按钮右侧
                        dropdown.style.top = `${buttonRect.bottom + 5}px`;
                        dropdown.style.right = `${window.innerWidth - buttonRect.right}px`;
                        dropdown.style.left = 'auto';
                    }
                });
            });
        });
    </script>
    <script>
        // 修复下拉菜单位置移动的bug
        document.addEventListener('DOMContentLoaded', function() {
            // 初始绑定现有菜单
            bindDropdownPositioning();
            
            // 创建MutationObserver监听DOM变化，为新添加的菜单绑定事件
            const observer = new MutationObserver(function(mutations) {
                mutations.forEach(function(mutation) {
                    if (mutation.addedNodes.length) {
                        bindDropdownPositioning();
                    }
                });
            });
            
            // 监听聊天列表容器的变化
            const chatList = document.querySelector('.chat-list');
            if (chatList) {
                observer.observe(chatList, { childList: true, subtree: true });
            }
            
            // 添加点击文档关闭菜单的事件
            document.addEventListener('click', function(event) {
                // 如果点击的不是菜单按钮或菜单内容
                if (!event.target.closest('.chat-item-menu') && !event.target.closest('.dropdown-menu')) {
                    // 关闭所有打开的菜单
                    document.querySelectorAll('.dropdown-menu.show').forEach(menu => {
                        menu.classList.remove('show');
                        // 找到对应的按钮并更新aria-expanded属性
                        const button = menu.previousElementSibling;
                        if (button && button.classList.contains('chat-item-menu')) {
                            button.setAttribute('aria-expanded', 'false');
                        }
                    });
                }
            });
        });
        
        // 绑定下拉菜单定位函数
        function bindDropdownPositioning() {
            const menuButtons = document.querySelectorAll('.chat-item-menu[data-bs-toggle="dropdown"]');
            
            menuButtons.forEach(button => {
                // 移除已有的事件监听器，避免重复绑定
                button.removeEventListener('show.bs.dropdown', positionDropdown);
                
                // 在菜单显示前设置位置
                button.addEventListener('show.bs.dropdown', positionDropdown);
            });
        }
        
        // 定位下拉菜单的函数
        function positionDropdown(event) {
            // 阻止Bootstrap的默认定位
            event.preventDefault();
            
            const button = this;
            const dropdown = button.nextElementSibling;
            
            if (dropdown && dropdown.classList.contains('dropdown-menu')) {
                // 获取按钮位置
                const buttonRect = button.getBoundingClientRect();
                
                // 临时设置菜单可见以获取其高度
                dropdown.style.visibility = 'hidden';
                dropdown.style.display = 'block';
                const dropdownHeight = dropdown.offsetHeight;
                dropdown.style.display = '';
                dropdown.style.visibility = '';
                
                // 检查是否会超出视口底部
                const viewportHeight = window.innerHeight;
                const spaceBelow = viewportHeight - buttonRect.bottom;
                const spaceAbove = buttonRect.top;
                const showAbove = spaceBelow < dropdownHeight && spaceAbove > dropdownHeight;
                
                // 设置菜单位置
                dropdown.style.position = 'fixed';
                
                if (showAbove) {
                    // 如果下方空间不足且上方空间足够，则显示在按钮上方
                    dropdown.style.top = `${buttonRect.top - dropdownHeight - 5}px`;
                    dropdown.classList.add('dropdown-menu-up'); // 可以添加一个类用于样式调整
                } else {
                    // 否则显示在按钮下方
                    dropdown.style.top = `${buttonRect.bottom + 5}px`;
                    dropdown.classList.remove('dropdown-menu-up');
                }
                
                dropdown.style.right = `${window.innerWidth - buttonRect.right}px`;
                dropdown.style.left = 'auto';
                
                // 关闭其他已打开的菜单
                document.querySelectorAll('.dropdown-menu.show').forEach(menu => {
                    if (menu !== dropdown) {
                        menu.classList.remove('show');
                        const menuButton = menu.previousElementSibling;
                        if (menuButton && menuButton.classList.contains('chat-item-menu')) {
                            menuButton.setAttribute('aria-expanded', 'false');
                        }
                    }
                });
                
                // 手动显示下拉菜单
                setTimeout(() => {
                    dropdown.classList.add('show');
                    button.setAttribute('aria-expanded', 'true');
                }, 0);
            }
        }
    </script>
</body>
</html>